from django.http import HttpResponseForbidden
from django.utils.deprecation import MiddlewareMixin


class OneMiddleware(MiddlewareMixin):
    def process_request(self, request):
        print('One----处理请求对象之前:', request.path)
        if request.path == '/users/':
            return HttpResponseForbidden('被拦截了!')

    def process_response(self, request, response):
        print('One----处理请求对象之后:', response.content)
        response.content = "66666666"
        return response


class TwoMiddleware(MiddlewareMixin):

    def process_request(self, request):
        print('Two2222-处理请求对象之前:', request.path)


    def process_response(self, request, response):
        print('Two2222-处理请求对象之后:', response.content)
        response.content = "66666666"
        return response


# 中间件的本质 是装饰器 --装饰器的本质: 闭包(函数嵌套函数)
def threemiddlware(get_response):
    def func(request):
        print('处理请求对象之前:', request.path)
        response = get_response(request)
        print('处理请求对象之后:', response.content)
        return response

    return func
